/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.base.controller;

import java.util.Collection;
import java.util.List;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;

import com.xy.biz.base.domain.resp.GetSupplierResponse;
import com.xy.biz.base.domain.req.GetSupplierRequest;
import com.xy.biz.base.domain.req.ListSupplierRequest;
import com.xy.biz.base.domain.req.SaveSupplierRequest;
import com.xy.biz.base.domain.query.SupplierQuery;
import com.xy.biz.base.domain.converter.SupplierConverter;
import com.xy.biz.base.client.SupplierClient;
import com.xy.biz.base.service.SupplierService;

/**
 * 
 * 
 * @author zgy
 * @date 2021-9-22
 */
@Api(tags = "供应商服务")
@Slf4j
@RestController
@RequestMapping("/api/base/supplier")
public class SupplierController implements SupplierClient {

	@Autowired
	private SupplierService supplierService;
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save")
	public Result<?> saveSupplier(@RequestBody SaveSupplierRequest request) {
		supplierService.saveSupplier(SupplierConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable(groups = {SaveChecks.class})
	@PostMapping(value = "/save_all")
	public Result<?> saveAllSupplier(@RequestBody Collection<SaveSupplierRequest> collection) {
		supplierService.saveAllSupplier(SupplierConverter.toBO(collection));
		return Result.success();
	}
	
	@Validatable(groups = {UpdateChecks.class})
	@PostMapping(value = "/update")
	public Result<?> updateSupplier(@RequestBody SaveSupplierRequest request) {
		supplierService.updateSupplier(SupplierConverter.toBO(request));
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/delete")
	public Result<?> deleteSupplier(@RequestBody GetSupplierRequest request) {
		supplierService.deleteSupplier(request.getSupplierId());
		return Result.success();
	}
	
	@Validatable
	@PostMapping(value = "/get")
	@Override
	public Result<GetSupplierResponse> getSupplier(@RequestBody GetSupplierRequest request) {
		return Result.success(SupplierConverter.toResponse(supplierService.getSupplier(request.getSupplierId())));
	}
	
	@PostMapping(value = "/list_by_ids")
	@Override
	public Result<List<GetSupplierResponse>> listSupplierById(@RequestBody Collection<Long> collection) {
		return Result.success(SupplierConverter.toResponse(supplierService.listSupplierById(collection)));
	}
	
	@PostMapping(value = "/list")
	@Override
	public Result<List<GetSupplierResponse>> listSupplier(@RequestBody ListSupplierRequest request) {
		return Result.success(SupplierConverter.toResponse(supplierService.listSupplier(SupplierConverter.toQuery(request))));
	}
	
	@PostMapping(value = "/count")
	@Override
	public Result<Integer> countSupplier(@RequestBody ListSupplierRequest request) {
		return Result.success(supplierService.countSupplier(SupplierConverter.toQuery(request)));
	}
	
	@PostMapping(value = "/page")
	@Override
	public Result<Page<GetSupplierResponse>> pageSupplier(@RequestBody ListSupplierRequest request) {
		SupplierQuery query = SupplierConverter.toQuery(request);
		List<GetSupplierResponse> list = SupplierConverter.toResponse(supplierService.listSupplier(query));
		int count = supplierService.countSupplier(query);
		return Result.success(new Page<>(list, count));
	}
	
}
